import { defineStore } from 'pinia'
import { useRouter } from 'vue-router'


export const useUserStore = defineStore('user', {
  state: () => ({
    user: JSON.parse(localStorage.getItem('user')) || null
    //token: null
  }),
  actions: {
    loginSuccess(user) {
      this.user = user
      localStorage.setItem('user', JSON.stringify(user))

    },
    logout() {
      this.user = null;
      localStorage.removeItem('user');
      localStorage.removeItem('token');
    }
  },
  getters: {
    currentUser: (state) => state.user
    
  }
})

/**
 * 使用示例
 * <script setup>
import { useUserStore } from '@/stores/userStore'

const userStore = useUserStore()

// 获取用户信息
const user = userStore.currentUser

// 检查是否已登录
const isLoggedIn = userStore.isLoggedIn
</script>
 */